using System;
using ClimbTracker.BusinessLogic.Entities;
using NHibernate;

namespace ClimbTracker.BusinessLogic.Dao
{
    /// <summary>
    /// Summary description for CompetitorDao.
    /// </summary>
    public class CompetitorDao : BaseDao
    {
        public CompetitorDao()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Retrieve a Competitor object from the database by his\her ID
        /// </summary>
        /// <param name="competitorID"></param>
        /// <returns></returns>
        public Competitor retrieveCompetitorByID(int competitorID)
        {
            Competitor competitor = null;
            try
            {
                competitor = (Competitor)currentSession.Get(typeof(Competitor), competitorID);
            }
            catch (HibernateException ex)
            {
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }

            return competitor;

        }

        /// <summary>
        /// Create a new competitor entry in the database from the given Competitor object
        /// </summary>
        /// <param name="competitor"></param>
        public void createCompetitor(Competitor competitor)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Save(competitor);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Delete a the database entry of the given Competitor object
        /// </summary>
        /// <param name="competitor"></param>
        public void deleteCompetitor(Competitor competitor)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Delete(competitor);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Update the database entry of the given Competitor object
        /// </summary>
        /// <param name="competitor"></param>
        public void updateCompetitor(Competitor competitor)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Update(competitor);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }



    }
}
